<?php

require '../data/data.php';

session_start();

$isSignin = isset($_SESSION['user'])? true : false;
if (!$isSignin) {
	header('Location:../public/signin.php');
	return;
}
$user = isset($_SESSION['user'])?$_SESSION['user'] : '';
$id = isset($_GET['id'])?$_GET['id'] : -1;
$cart = isset($_SESSION['cart'])?$_SESSION['cart']:null;

$isExistsInCart = false;
for ($i=0; $i < count($cart); $i++) { 
	if ($cart[$i]['id'] == $id) {
		$cart[$i]['num'] = $cart[$i]['num'] + 1;
    	$isExistsInCart  = true;
    	break;
	}
}
if (!$isExistsInCart) {
    $good = query('select * from goods where id=?',[$id])[0];
    $good['num'] = 1;
    $cart[] = $good;
}
$_SESSION['cart'] = $cart;

header('Location:../public/index.php');

